package com.alumni.dbconnection;

import java.sql.*;

public class Condb {


    ResultSet rs = null;
    Connection con = null;
    Statement stmt = null;
    DBConnect dbc = new DBConnect();
    public Condb() {
        
        con = dbc.getConnection();
   
    }

    public int getRowCount(String strSql){
        int intCount = 0;
        try{
            rs = this.executeQuery("SELECT COUNT(*) FROM " +strSql);
            if(rs.next()){
                intCount = rs.getInt(1);
            }
            else{
                intCount = -1;
            }
        }catch(Exception e){
            intCount = -2;
            System.err.println(e.getMessage());
            e.printStackTrace();
        }finally{
           
        }
        return intCount;

    }

    public ResultSet executeQuery(String sql){
        try{
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);

        }catch(SQLException e2){
            System.err.print(e2);
        }
        return rs;
    }

    public ResultSet getWriteSet(String sql){
        try{
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery(sql);

        }catch(SQLException e2){
            System.err.print(e2);
        }
        return rs;

    }


    public int executeUpdate(String sql){
        int rt = 0;
        try{
            stmt = con.createStatement();
            rt = stmt.executeUpdate(sql);
        }catch(SQLException ex){
            System.err.print(ex);
        }
        return rt;
    }

    public void close(){
        try{
            if(con!=null){
                con.close();
                dbc.close();
            }
            
        }catch(Exception e){
            System.out.print(e);
        }try{
            if(rs!=null)
                rs.close();
        }catch(Exception e){
            System.out.println(e);
        }
    }

}

